数据结构MOOC 02-线性结构4 Pop Sequence
02-线性结构 4 Pop Sequence
题目要求
Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
Output Specification:
For each pop sequence, print in one line “YES” if it is indeed a possible pop sequence of the stack, or “NO” if not.
Sample Input:
1 | 5 7 5 |
Sample Output:
1 | YES |
Solution:
题目要求是按照 1, 2, 3, …, N 的顺序将数字压入栈中,但是会随机在某一时刻弹出,要求我们可以判断给出的出栈序列是否符合栈的入栈出栈规范,是否可能实现,是的话输出 “YES”,否输出“NO”。
解决方法主要就是模拟入栈和出栈的过程,在此过程中一旦发现违反栈 “先进后出” 的规则(栈顶元素大于当前弹出元素),或者栈溢出,就证明该种情况不成立;若将给出的测试序列全部走完未发现问题,则证明该种情况可能实现。
程序第一行要求输入三个数据:M(栈的最大容量)、N(每个输入序列的数据个数)、K(待测试的序列个数),接下来输入 K 行待检测序列,开始模拟入栈出栈操作。
Code:
1 |
|